package com.xzc.service;

import com.xzc.common.Rule;
import com.xzc.entity.Box;
import com.xzc.mapper.BoxMapper;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @auther kaka
 * @Date 2020-06-16
 */
@Service
public class BoxService {

    @Autowired
    private BoxMapper boxMapper;
    @Autowired
    private Rule rule;


    public List<Box> getOrderBox(String orderId) {
        return boxMapper.getByOrderId(orderId);
    }

    public String split(String orderId, int boxId,int partNums) {
        List<Box> boxes = boxMapper.getByOrderId(orderId);
        for (Box box : boxes) {
            if (box.getFatherId() == boxId) {
                return "boxId error";
            }
        }
        for (Box box : boxes) {
            if (box.getBoxId() == boxId) {
                for (int i = 0; i < partNums; i++) {
                    Integer part = Integer.valueOf(rule.getPart().split(",")[i]);
                    boxMapper.addBox(orderId, new Box()
                            .setNums((box.getNums() * part / 100))
                            .setFatherId(boxId));
                }
                break;
            }
        }

        return null;
    }

    //    @Transactional Use Transactional to ensure consistency
    public String merge(String orderId, List<String> boxIds) {
        List<Box> boxList = new ArrayList<>();
        for (String boxId : boxIds) {
            Box box = boxMapper.getBoxByBoxId(orderId, Integer.parseInt(boxId));
            if (box == null) {
                return "error";
            }
            boxList.add(box);
        }
        if (boxList.size() == boxIds.size()) {
            //1.del old box
            boxMapper.delBox(orderId, boxIds);

            //2.cal nums
            int sumNum = boxList.stream().collect(Collectors.summingInt(Box::getNums));

            //3.merge box
            Box box = new Box()
                    .setNums(sumNum)
                    .setFatherId(boxList.get(0).getFatherId());
            boxMapper.addBox(orderId, box);

            //merge +1 = goods.size  only delBox
        } else if (boxList.size() + 1 == boxMapper.getByOrderId(orderId).size()) {
            boxMapper.delBox(orderId, boxIds);
        } else {
            return "box not found ";
        }

        return null;
    }

    public String increase(String orderId, int num) {
        int rootNum = 0;
        List<Box> boxList = boxMapper.getByOrderId(orderId);
        if (boxList.isEmpty()) {
            return "error";
        } else {
            for (Box box : boxList) {
                if (box.getFatherId() == 0) {
                    rootNum = box.getNums();
                    box.setNums(box.getNums() + num);
                } else {
                    double per = (double) (rootNum + num) / rootNum;
                    box.setNums((int) (box.getNums() * per));
                }
            }
            return null;
        }
    }
}
